<?php
header("Access-Control-Allow-Origin: *");

include_once("./utils/db.php");
include_once("./utils/functions.php");
$ajaxReturn = ["code" => 0, "msg" => ""];
$course = [];
if (isset($_POST["action"])) {
  //修改课程
  if ($_POST['action'] == 'update') {
    $course["courseid"] = $_POST["courseid"];
    $course["coursename"] = $_POST["coursename"];
    $course["credit"] = $_POST["credit"];
    $course["period"] = $_POST["period"];
    $course["content"] = $_POST["content"];
    echo updateCourse($conn, $course);
  }
  //增加课程
  if ($_POST['action'] == 'add') {
    $course["courseid"] = $_POST["courseid"];
    $course["coursename"] = $_POST["coursename"];
    $course["credit"] = $_POST["credit"];
    $course["period"] = $_POST["period"];
    $course["content"] = $_POST["content"];
    echo saveCourse($conn, $course);
  }
  //课程查询
  if ($_POST['action'] == 'queryCourse') {
    if (isset($_POST['courseid'])) {
      $course["courseid"] = $_POST["courseid"];
    }
    if (isset($_POST["coursename"])) {
      $course["coursename"] = $_POST["coursename"];
    }
    if (isset($_POST["credit"])) {
      $course["credit"] = $_POST["credit"];
    }
    echo queryCourse($conn, $course);
  }
}
if (isset($_GET["action"])) {
  //获取课程信息
  if ($_GET['action'] == 'query') {

    echo getCourses($conn);
  }
  //删除课程信息
  if ($_GET['action'] == 'delete') {
    $courseid = $_GET['courseid'];
    echo deleteCourse($conn, $courseid);
  }
}
//获取所有课程信息
function getCourses($conn)
{
  $sql = "select * from xk_course";

  $courses = [];
  $results = mysqli_query($conn, $sql);

  while ($row = mysqli_fetch_assoc($results)) {
    $courses[] = $row;
  }
  global $ajaxReturn;
  $ajaxReturn = ["code" => 200, "msg" => "查询成功", "data" => $courses];
  return json_encode($ajaxReturn, JSON_UNESCAPED_UNICODE);
}
//增加课程
function saveCourse($conn, $course)
{
  global $ajaxReturn;
  //sql
  $sql = "insert into xk_course(coursename,credit,period,departmentid,content) values(?,?,?,?,?)";
  //使用prepare方法插入数据
  $stmt = $conn->prepare($sql);
  //bind-param 中s代表字符串，i代表整型
  $stmt->bind_param(
    "siiis",
    $course['courseName'],
    $course['credit'],
    $course['period'],
    $course['departmentid'],
    $course['content']
  );
  //执行的结果如果小于等于0表示数据插入失败，如果为1 表示插入成功
  $result = $stmt->execute();
  if ($result === true) {
    $ajaxReturn = ['code' => 200, 'msg' => '数据插入成功'];
  } else {
    $ajaxReturn = ['code' => 0, 'msg' => '数据插入失败'];
  }
  return json_encode($ajaxReturn, JSON_UNESCAPED_UNICODE);
}
//修改课程
function updateCourse($conn, $course)
{
  global $ajaxReturn;
  //3. sql
  $sql = "update xk_course set coursename=?,credit=?, period=?, content=? where courseid=?";
  //4. 创建stmt
  $stmt = mysqli_prepare($conn, $sql);

  //6. 绑定值
  $stmt->bind_param("siisi", $course['coursename'], $course['credit'], $course['period'], $course['content'], $course['courseid']);
  $result = $stmt->execute();
  if ($result === true) {
    $ajaxReturn = ['code' => 200, 'msg' => '修改成功'];
  } else {
    $ajaxReturn = ['code' => 0, 'msg' => '修改失败'];
  }
  return json_encode($ajaxReturn, JSON_UNESCAPED_UNICODE);
}

//删除课程
function deleteCourse($conn, $courseid)
{
  global $ajaxReturn;
  $sql = "delete from xk_course where courseid=?";
  $stmt = mysqli_prepare($conn, $sql);
  $stmt->bind_param("i", $courseid);
  $result = $stmt->execute();
  if ($result === true) {
    $ajaxReturn = ["code" => 200, "msg" => "删除成功"];
  } else {
    $ajaxReturn = ["code" => 0, "msg" => "删除失败"];
  }
  return json_encode($ajaxReturn, JSON_UNESCAPED_UNICODE);
}
//课程查询
function queryCourse($conn, $course)
{
  //引入全局对象返回值
  global $ajaxReturn;
  //编写基本sql语句
  $base = "select * from xk_course ";
  //sql语句条件部分
  $condition = '';
  //1. 判断courseid是否存在？存在，只对courseid进行查询，
  if (isset($course["courseid"]) && $course["courseid"] > 0) {
    $condition .= " courseid= " . $course["courseid"];
  } else {
    //根据课程名称查询，模糊查询
    if (isset($course["coursename"]) && $course["coursename"] != '') {
      $condition .= " coursename like '%" . $course["coursename"] . "%'";
    }
    //根据学分
    if (isset($course["credit"]) && $course["credit"] != 0) {
      if ($condition != "")
        $condition .= " and  credit =" . $course["credit"];
      else
        $condition .= "  credit =" . $course["credit"];
    }
  }
  if ($condition != "") {
    $sql = $base . " where " . $condition;
  } else {
    $sql = $base;
  }
  $result = mysqli_query($conn, $sql);
  //判断结果集的数量
  $num = mysqli_num_rows($result);
  if ($num > 0) {
    //对结果集进行遍历,assoc, array ,num
    $c = [];
    while ($row = mysqli_fetch_assoc($result)) {
      $c[] = [
        "courseid" => $row['courseid'],
        "coursename" => $row["coursename"],
        "credit" => $row["credit"],
        "period" => $row["period"],
        "content" => $row["content"],
      ];
    }
    $ajaxReturn = ["code" => 200, "num" => $num, "msg" => "查询成功", "data" => $c];
  } else {
    $ajaxReturn = ["code" => 0, "num" => 0, "msg" => "无查询结果", "data" => null];
  }
  //输出给客户端json数据
  return json_encode($ajaxReturn, JSON_UNESCAPED_UNICODE);
}
